home *** CD-ROM | disk | FTP | other *** search
- Path: monet.ccs.itd.umich.edu!dogcow
- From: dogcow@monet.ccs.itd.umich.edu (Tom Spindler)
- Newsgroups: comp.lang.c
- Subject: Extracting chars from shorts.
- Date: 10 Jan 1996 19:20:32 GMT
- Organization: University of Michigan
- Message-ID: <4d13i0$k3s@lastactionhero.rs.itd.umich.edu>
- NNTP-Posting-Host: monet.ccs.itd.umich.edu
-
- I'm trying to extracts individual bytes from a double. The following code
- doesn't work:
-
- typedef unsigned char zbyte; /* unsigned 1 byte quantity */
- typedef unsigned short zword; /* unsigned 2 byte quantity */
-
- typedef union zw_u_t {
- zword zword;
- struct {zbyte hi,lo;} zbyte;
- } zwordun;
-
- #define lo(v) ((zbyte) (((zwordun) v).zbyte.lo))
- #define hi(v) ((zbyte) (((zwordun) v).zbyte.hi))
-
- main() {
- zword foon;
- zbyte zfoo;
-
- zfoo = lo(foon);
- }
-
- One compiler complains that it's an illegal cast, whereas gcc says that the
- return value is an int.
-
- I could do something like ((char *)&v[0]), but then it would crash if I
- tried to do something with lo(42).
-
- Any suggestions? (I already know what endian the code is supposed to run on.)
-
-